package com.bootlever.framework.core.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class BootleverApplicationContext implements ApplicationContextAware, EnvironmentAware {

    private static ApplicationContext context;

    private static Environment environment;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    public static ApplicationContext getContext() {
        return context;
    }

    public static Object getBean(String name) {
        return getContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz){
        return getContext().getBean(clazz);
    }

    public static <T> T getBean(String name,Class<T> clazz){
        return getContext().getBean(name,clazz);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    public static String getProperty(String key){
        return environment.getProperty(key);
    }
}
